/* 
 * $Id: command.c 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 * Copyright (c) 2009 Prabhu Gurumurthy <pgurumur@gmail.com>
 *  
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "eltclsh.h"
#include "command.h"

int
exitcode(ClientData data, Tcl_Interp *tcl, int argc, char *argv[])
{
   ElTclInterpInfo *iinfo = data;
   argc = argc;
   argv = argv;
   tcl = tcl;

   Tcl_SetObjResult(iinfo->interp, Tcl_NewIntObj((long)iinfo->errcode));
   return TCL_OK;
}

int
getumask(ClientData data, Tcl_Interp *tcl, int argc, char *argv[])
{
   int retval = TCL_ERROR;
   ElTclInterpInfo *temp = (ElTclInterpInfo *)data;
   if (argc > 2)
      Tcl_SetResult(tcl, "wrong # args, need 1 or 2", TCL_STATIC);
   else {
      if (argc == 2) {
         if (strcmp(argv[1], "symbolic") == 0 || strcmp(argv[1], "sym") == 0) {
            mode_t mask = temp->mask;
            char buf[18];         
            int jy, ix = 0;
           
            mask = ~mask;
            char *cp = buf;             
            for (ix = 0; ix < 3; ix++) {
               *cp++ = "ugo"[ix];
               *cp++ = '=';
               for (jy = 0; jy < 3; jy++)
                  if (mask & (1 << (8 - (3*ix + jy))))
                     *cp++ = "rwx"[jy];
               *cp++ = ',';       
            } 
            cp[-1] = '\0';        
            Tcl_SetResult(tcl, buf, TCL_VOLATILE);
            retval = TCL_OK;
         }
      }
      else if (argc == 1) {
         char buf[BUFSIZ];
         memset(buf, '\0', BUFSIZ);
         snprintf(buf, BUFSIZ, "%#3.3o", temp->mask);
         Tcl_SetResult(tcl, buf, TCL_VOLATILE);
         retval = TCL_OK;
      }
   }

   return retval;
}


